package top.yangyitao.service.impl;


import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.yangyitao.constant.LoginCheckResult;
import top.yangyitao.constant.RegisterCheckResult;
import top.yangyitao.constant.ResetPWDResult;
import top.yangyitao.dao.RegisterCodeDao;
import top.yangyitao.dao.UserDao;
import top.yangyitao.model.RegisterCode;
import top.yangyitao.model.User;
import top.yangyitao.service.UserService;
import top.yangyitao.util.RegisterEmailUtil;
import top.yangyitao.util.ResetPWDEmailUtil;

@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {

	@Resource
	private UserDao userDao;
	@Resource
	private RegisterCodeDao registerCodeDao;
	@Override
	public LoginCheckResult userLoginCheck(User user) {	
		User user0 = userDao.findUserByUserName(user.getUserName());
		User user1 = userDao.findUserByEmail(user.getUserName());
		if(user1!=null)
			user0 = user1;
		if(user0==null) 
			return LoginCheckResult.USERNOTEXIT;
		else if(user0.getPassword().equals(user.getPassword())) {
			if(user0.getFlag()==1)
				return LoginCheckResult.SUCCESS;
			else
				return LoginCheckResult.UNACTIVE;
		}
		else
			return LoginCheckResult.WRONGPWD;
	}
	@Override
	public RegisterCheckResult userRegisterCheck(User user) {
		User user0 = userDao.findUserByEmail(user.getEmail());
		if(user0!=null) {
			if(user0.getFlag()!=0)
				return RegisterCheckResult.EMAILEXIT;
			else {
				userDao.deleteUserByUserName(user0.getUserName());
				registerCodeDao.deleteRegisterCodeByUserName(user0.getUserName());
				try {
					RegisterCode registerCode = registerCodeDao.createRegisterCodeByUserName(user.getUserName());
					RegisterEmailUtil.sendRegisterEmail(user.getEmail(), registerCode);
					return RegisterCheckResult.REREGISTER;
				}catch(Exception e) {
					e.printStackTrace();
					return RegisterCheckResult.EMAILERROR;	
				}
			}
			}
		if(userDao.findUserByUserName(user.getUserName())!=null)
			return RegisterCheckResult.USERNAMEEXIT;
		else {
			try {
				RegisterCode registerCode = registerCodeDao.createRegisterCodeByUserName(user.getUserName());
				RegisterEmailUtil.sendRegisterEmail(user.getEmail(), registerCode);
				return RegisterCheckResult.SUCCESS;
			}catch(Exception e) {
				e.printStackTrace();
				return RegisterCheckResult.EMAILERROR;	
			}
		}
	}
	@Override
	public void insertUnactiveUser(User user) {
		user.setFlag(0);
		userDao.insertUser(user);
	}
	@Override
	public void activeUser(String userName) {
		userDao.activeUser(userName);
		registerCodeDao.updateRegisterCodeByUserName(userName);
	}
	@Override
	public ResetPWDResult resetUserPWDByUserName(String userName) {
		User user = userDao.findUserByUserName(userName);
		if(user==null)
			return ResetPWDResult.USERNOTEXIT;
		else if(user.getFlag()==0)
			return ResetPWDResult.UNACTIVE;
		else {
			registerCodeDao.updateRegisterCodeByUserName(userName);
			try {
				RegisterCode registerCode = registerCodeDao.findRegisterCodeByUserName(userName);
				ResetPWDEmailUtil.sendResetPWDEmail(user.getEmail(), registerCode);
				return ResetPWDResult.SUCCESS;
			}catch(Exception e) {
				e.printStackTrace();
				return ResetPWDResult.EMAILERROR;
			}
		}
	}
	@Override
	public void updateUserOfPassword(User user) {
		userDao.updateUserOfPassword(user);
	}
}
